/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';

import Pager from '../../../../grsc-base/component/Pager.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import Loading from '../../../../grsc-base/component/Loading.vue';
import commonService from '../../../common/commonService';
import {PAGE_SIZE} from '../../../../grsc-base/common/constants';
import tpl from './tpl.html';

export default Vue.extend({
    name: "Message",
    template: tpl,
    components: {
        Pager,
        Loading,
        NoData
    },
    data() {
        return {
            type: -1,
            list: [],
            isLoading: true,
            pageNo: 1,
            pageNum: 0,
            tempShow: ""
        }
    },
    created() {
        this.type = this.$route.params.type;
        this.fetchList();
    },
    filters: {
        textFilter(val) {
          return val.replace('￥',':')
        }, 
      },
    watch: {
        '$route.params.type'(newVal, oldVal) {
            this.type = newVal;
            this.pageNo = 1;
            this.fetchList();
        }
    },
    methods: {
        /**
         * 获取数据列表
         */
        fetchList() {
            this.isLoading = false;
            let config = {};
            if (this.type === 'asset') {
                config = {
                    "category": "ASSETS",
                    pageNo: this.pageNo,
                    pageSize: PAGE_SIZE
                }
            } else if (this.type === 'safe') {
                config = {
                    "category": "SECURITY",
                    pageNo: this.pageNo,
                    pageSize: PAGE_SIZE
                }
            } else {
                config = {
                    pageNo: this.pageNo,
                    pageSize: PAGE_SIZE
                }
            }
            commonService.accountMessageList(config).done((rep) => {
                this.isLoading = false;
                if (rep.code == window.CODE.SUCCESS) {
                    this.isLoading = false;
                    this.pageNum = rep.data.pageNum;
                    this.list = this._renderList(rep.data.list);
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {

            });
        },
        //渲染列表数据
        _renderList(list){
            if (list.length === 0) {
                return [];
            }
            let listObj = {
                item_name: "",
                item_time: "",
                item_txt: ""
            };
            let newList = [];
            let vm = this;
            list.forEach(function (value) {
                let tmpName = value.userMessageCategory;
                if (tmpName === "ASSETS") {
                    tmpName = vm.$tc('message.typeOption', 1);
                }
                else if (tmpName === "SECURITY") {
                    tmpName = vm.$tc('message.typeOption', 2);
                }
                newList.push({
                    item_name: tmpName,
                    item_time: value.createDate,
                    item_txt: value.content
                })
            });
            return newList;
        },

        /**
         * 改变分页
         */
        changePage(pageNo) {
            this.pageNo = pageNo;
            this.fetchList()
        }
    }

});